package cn.edu.scau.cmil.linwei.comprehensive.Controller;

import cn.edu.scau.cmil.linwei.comprehensive.Script.DataField;
import cn.edu.scau.cmil.linwei.comprehensive.Script.User;
import cn.edu.scau.cmil.linwei.comprehensive.util.DataIO;
import cn.edu.scau.cmil.linwei.comprehensive.util.StageManager;
import javafx.application.Platform;
import javafx.fxml.Initializable;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;

public class ForgetPWController implements Initializable {

    public BorderPane borderPane;

    public TextField IDTextField;
    public Text InitQuestionText;
    public TextField InitAnswerTextField;
    public PasswordField newPassWordField;
    public Text TipText;

    public void GoBack(){//返回登录界面
        timer.cancel();
        CloseForgetPW();//关闭当前界面
        openLoginWindows();//打开登录界面
    }//返回

    public void CloseForgetPW(){
        Stage stage = (Stage)borderPane.getScene().getWindow();//通过布局获取场景再获取舞台
        StageManager.STAGEMAP.put("ForgetPW",stage);//保存舞台
        StageManager.CONTROLLERMAP.put("ForgetPWController",this);//保存控制器
        stage.close();//关闭舞台
    }//关闭当前页面

    public void openLoginWindows() {
        Stage stage = StageManager.STAGEMAP.get("Login");//读取Login的引用，打开Login界面
        StageManager.STAGEMAP.remove("Login");//在MAP中删除Login页面的Stage
        StageManager.CONTROLLERMAP.remove("Login");//在MAP中删除Login页面的Controller
        stage.show();
    }//回到登陆界面

    User TargetUser;

    public void ForgetPWOnAction() throws IOException {
        String answer = InitAnswerTextField.getText();
        String newPW = newPassWordField.getText();
        if(answer.equals(TargetUser.InitAnswer)){//如果密保答案相等
            if(newPW.length()>10){
                TipText.setText("密码长度不能大于10");
            }
            else if(newPW.length() == 0){
                TipText.setText("密码不能为空");
            }
            else{//密保答案正确而且密码长度合格
                TargetUser.PW = newPW;
                DataIO.UserWrite();
                IDTextField.clear();
                InitQuestionText.setFill(Color.BLACK);
                InitQuestionText.setText("密保问题：");
                InitAnswerTextField.clear();
                newPassWordField.clear();
                TipText.setText("密码修改成功");
            }
        }
        else{
            TipText.setText("密保答案错误");
        }
    }//点击确定按钮，验证密保答案和新答案

    //标题栏最小化、关闭、拖拽
    private double oldStageX,oldStageY,oldScreenX,oldScreenY;//点击时记录窗口的XY
    //最小化
    public void MinButtonClicked(){
        Stage stage = (Stage) borderPane.getScene().getWindow();
        stage.setIconified(true);
    }//设置最小化按钮点击时发生的活动选项
    //关闭
    public void CloseButtonClicked(){
        Stage stage = (Stage) borderPane.getScene().getWindow();
        timer.cancel();
        stage.close();
    }//设置关闭按钮点击时发生的活动选项
    //鼠标拖拽
    public void MousePressed(MouseEvent event){
        Stage stage = (Stage) borderPane.getScene().getWindow();
        oldStageX = stage.getX();//事件相对于MouseEvent源的原点的水平位置，即获取当前鼠标位置
        oldStageY = stage.getY();//事件相对于MouseEvent源的原点的垂直位置，即获取当前鼠标位置
        oldScreenX = event.getScreenX();//返回事件的绝对水平位置，即获取窗口左上角位置
        oldScreenY = event.getScreenY();//返回事件的绝对垂直位置，即获取窗口左上角位置
    }

    public void MouseDragged(MouseEvent event){
        Stage stage = (Stage) borderPane.getScene().getWindow();
        stage.setX(event.getScreenX()-oldScreenX+oldStageX);
        stage.setY(event.getScreenY()-oldScreenY+oldStageY);
    }

    //界面实时刷新
    Timer timer;
    boolean onfire = false;

    public void updateInitQuestionText(){
        if(onfire){//如果为真，证明上一次update还没有结束，不进行下一次更新
            return;
        }
        onfire = true;
        String ID = IDTextField.getText();
        if(ID.equals("")){//输入为空，return
            onfire = false;//熄火
            return;
        }
        User user;
        for(int i = 0;i< DataField.userData.size();i++){
            user = DataField.userData.get(i);
            if(user.ID.equals(ID)){
                TargetUser = user;
                InitQuestionText.setText("密保问题："+user.InitQuestion);
                onfire = false;//熄火
                return;
            }
        }
        //如果能到这，证明找不到用户且输入不为空，没有return
        InitQuestionText.setText("查找不到用户\""+ID+"\"，请检查输入的帐号");
        onfire = false;//熄火
    }//更新密保问题，将找到的用户赋值给TargetUser

    public void update(){
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        updateInitQuestionText();
                    }
                });
            }
        };
        timer = new Timer();
        timer.schedule(timerTask,0,1000);
    }//建立新线程执行相关的操作，每1秒调用一次updateInitQuestionText()

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        TipText.setText("");
        update();
    }
}
